
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module protocol
* @author: Hjava
* @description: 协议编码和解码模块
* @since: 2018-07-16 11:32:10
*/
var packet_1 = __webpack_require__(/*! _util/packet */ "./src/core/utils/packet.ts");
var byteBuffer_1 = __webpack_require__(/*! _util/byteBuffer */ "./src/core/utils/byteBuffer.ts");
var base64_1 = __webpack_require__(/*! _util/base64 */ "./src/core/utils/base64.ts");
var bufferType_1 = __webpack_require__(/*! _util/bufferType */ "./src/core/utils/bufferType.ts");
var eventEmitter_1 = __webpack_require__(/*! _util/eventEmitter */ "./src/core/utils/eventEmitter.ts");
var extend_1 = __webpack_require__(/*! _util/extend */ "./src/core/utils/extend.ts");
var constant_1 = __webpack_require__(/*! _util/constant */ "./src/core/utils/constant.ts");
var userData_1 = __webpack_require__(/*! ./userData */ "./src/core/utils/userData.ts");
var Protocol = {};
var packetMap = {};
function packUp(uri, bean) {
    if (typeof uri == 'number') {
        packetMap[uri] = bean;
    }
    else {
        for (var i = 0; i < uri.length; i++) {
            packetMap[uri[i]] = bean;
        }
    }
    return bean;
}
function getHeader(a, version) {
    if (version === void 0) { version = 1; }
    return __assign({ length: (a[3]) + (a[2] << 8) + (a[1] << 16) + (a[0] << 24), uri: (a[7]) + (a[6] << 8) + (a[5] << 16) + (a[4] << 24), appid: (a[9]) + (a[8] << 8), buffer: {
            array: a
        } }, (version > 1 ? {
        mversion: (a[11]) + (a[10] << 8),
        crc32: (a[15]) + (a[14] << 8) + (a[13] << 16) + (a[12] << 24),
        requestSeq: (a[19]) + (a[18] << 8) + (a[17] << 16) + (a[16] << 24),
        extra: (a[23]) + (a[22] << 8) + (a[21] << 16) + (a[20] << 24)
    } : {}));
}
function unmarshall(buffer, unknown, shouldLogReport, version) {
    if (version === void 0) { version = 1; }
    buffer = byteBuffer_1.default.wrap(buffer);
    var header = getHeader(buffer.array, version);
    var Packet = packetMap[header.uri];
    if (Packet) {
        try {
            var packet = new Packet().fromBuffer(buffer);
            return packet;
        }
        catch (e) {
            if (shouldLogReport) {
                throw new Error();
            }
        }
    }
    return __assign(__assign({}, unknown), header);
}
function mustImpl(method) {
    if (typeof this[method] === 'undefined') {
        throw new Error('[' + method + '] is required~');
    }
}
/**
 * Request 只能发不能收为request
 * @type {Function}
 */
Protocol.Request = extend_1.default(packet_1.default, {});
Protocol.Request.extend = function (o) {
    mustImpl.call(o, 'init');
    mustImpl.call(o, 'marshall');
    return extend_1.default(Protocol.Request, o);
};
/**
 * TransUp
 * @type {Function}
 */
Protocol.TransUp = extend_1.default(packet_1.default, {
    uri: constant_1.BASE_URI.URI_UNI_TRANSUP,
    init: function () {
        this.svid = 0;
        this.uid = 0;
        this.buf = null;
        this.flag = 0;
        this.seqId = 0;
        this.deviceId = userData_1.default.get('deviceId');
        this.traceId = 0;
        this.swimlane = userData_1.default.get('swimlane') || '';
    },
    /**
     * @return {ArrayBuffer}
     * @private
     */
    marshall: function () {
        this.push(this.svid, bufferType_1.default.SHORT); //2
        this.push(this.uid, bufferType_1.default.LONG); //8
        // 以上为 header 信息
        this.push(this.buf, bufferType_1.default.BYTES);
        this.push(this.flag, bufferType_1.default.BYTE);
        this.push(this.seqId, bufferType_1.default.INT);
        this.push(this.deviceId, bufferType_1.default.STRING);
        this.push(this.traceId, bufferType_1.default.LONG);
        this.push(this.swimlane, bufferType_1.default.STRING);
    },
    /**
     * @return {ArrayBuffer}
     * @private
     */
    unmarshall: function () {
        this.svid = this.popShort();
        this.uid = this.popLong().value();
        this.buf = this.popBytes();
        this.flag = this.popByte();
        this.seqId = this.popInt();
        this.deviceId = this.popString();
        this.traceId = this.popLong().value();
        this.swimlane = this.popString();
    },
    unserialize: function () {
        return unmarshall(this.buf);
    }
});
packUp(constant_1.BASE_URI.URI_UNI_TRANSUP, Protocol.TransUp);
/**
 * response 既可以收 又可以发设定为response
 * @type {Function}
 */
Protocol.Response = extend_1.default(packet_1.default, {
    toPure: function () {
        Protocol.Response.superclass.toPure.call(this);
        this.message && this.message.toPure && this.message.toPure();
        //有时候this.msgId 就是一个普通字符串,以后在跟进原因
        this['msgId'] && (this['msgId'] = (this['msgId'].value ?
            this['msgId'].value() : this['msgId']));
        return this;
    }
});
Protocol.Response.extend = function (o) {
    mustImpl.call(o, 'uri');
    mustImpl.call(o, 'unmarshall');
    return packUp(o.uri, extend_1.default(Protocol.Response, o));
};
Protocol.Response.fromBuffer = function (buffer, version) {
    if (version === void 0) { version = 1; }
    return unmarshall(buffer, {
        text: 'unknown response type',
        type: -1
    }, false, version);
};
/**
 * @param base64
 */
Protocol.Response.fromBase64 = function (base64Str, version) {
    if (version === void 0) { version = 1; }
    return Protocol.Response.fromBuffer(base64_1.default.b64toBA(base64Str).buffer, version);
};
Protocol.Message = extend_1.default(packet_1.default, {
    toPure: function () {
        Protocol.Message.superclass.toPure.call(this);
        return this;
    }
});
Protocol.Message.extend = function (o) {
    mustImpl.call(o, 'init');
    mustImpl.call(o, 'marshall');
    mustImpl.call(o, 'unmarshall');
    return packUp(o.uri, extend_1.default(Protocol.Message, o));
};
Protocol.Message.fromBuffer = function (buffer) {
    return unmarshall(buffer, {
        text: 'unknown message type',
        type: -1
    }, true);
};
eventEmitter_1.default.on('_dataRecv', function (req) {
    var packet = unmarshall(req, {
        text: 'unknown data type',
        type: -1
    });
    eventEmitter_1.default.emit(packet.uri, packet);
});
exports.default = Protocol;


//# sourceURL=webpack://MTDX/./src/core/utils/protocol.ts?